
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import sai.mysql.DriverLoading;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author sai
 */
public class Hospital extends HttpServlet
{
    PreparedStatement prepare, prepare1;
    Statement state;
    int deptid;    
    @Override
    public void doGet(HttpServletRequest req,HttpServletResponse res)throws ServletException,IOException
    {
        res.setContentType("text/html");
        PrintWriter out = res.getWriter();
        String doctorid=req.getParameter("doctorid");
        String hospname=req.getParameter("hospname");
        String location=req.getParameter("location");
        String department=req.getParameter("department");
        String ocontact=req.getParameter("ocontact");
        String con= ocontact.substring(ocontact.length() - 7, ocontact.length());
        String hospid = "hos" + con;
        Integer fromtime=Integer.parseInt(req.getParameter("time"));                
        Integer totime=Integer.parseInt(req.getParameter("time1"));                
        
        try
        {
            DriverLoading dl = new DriverLoading();
            
            state=dl.getStatement();
            ResultSet rs=state.executeQuery("select dept_id from `department` where dept_name='"+department+"'");
            boolean b=rs.next();           
            if(b)
            {
                deptid=rs.getInt(1);
            }
            out.println(deptid);
            prepare=dl.getPreparedStatement("insert into `hospital` values(?,?,?,?,?,?,?,?)");
            prepare.setString(1,hospid);
            prepare.setInt(2,deptid);
            prepare.setString(3,hospname);
            prepare.setString(4,location);
            prepare.setString(5,doctorid);
            prepare.setString(6,ocontact);
            prepare.setInt(7,fromtime);
            prepare.setInt(8,totime);
            
            int x=prepare.executeUpdate();
            if(x>0)
            {
                out.println("<center><font color='red' size='7'>success !!</font></center>");                
            }
            else
            {
                out.println("<center><font color='red' size='7'>please try again<a href='hospital.html'>click</a> </font></center>");                
            }   
        }
        catch(SQLException e)
        {
            System.out.println(e);
        }
        catch(Exception e)
        {
            System.out.println(e);
        }
    }    
}
